﻿<xsl:stylesheet version="1.0"
	xmlns:date="http://exslt.org/dates-and-times" xmlns:ebe="http://cks/ebemethods"
	xmlns:string="http://exslt.org/strings"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="date ebe">

	<xsl:param name="IsBlogOwner" />
	<xsl:param name="RelativeUrl" />
	<xsl:param name="QSpageindex" />
	<xsl:param name="TrimPostBodyOnLists"/>

	<xsl:output omit-xml-declaration="yes" />

	<xsl:template match="/">
		
		<xsl:variable name="endPos" select="number($QSpageindex) * 10"/>

		<xsl:choose>
			<!-- Is there a Query String for the page index -->
			<xsl:when test="$QSpageindex">
				<xsl:apply-templates select="rows/row[position() &gt; number($endPos)-10 and position() &lt; number($endPos)+1]"/>
			</xsl:when>
			<!-- Nope...just show the first 10 -->
			<xsl:otherwise>
				<xsl:apply-templates select="rows/row[position() &lt; 11]"/>
			</xsl:otherwise>
		</xsl:choose>

		<xsl:variable name="pos">
			<xsl:choose>
				<xsl:when test="not($QSpageindex)">1</xsl:when>
				<xsl:otherwise><xsl:value-of select="number($QSpageindex)"/></xsl:otherwise>
			</xsl:choose>
			
		</xsl:variable>
		
		<xsl:if test="number($endPos) &gt; 10">
			<!-- We are on another page...show the previous -->
			<p>
				<a>
					<xsl:attribute name="href">
						?PageIndex=<xsl:value-of select="number($pos)-1"/>
					</xsl:attribute>
					&lt;&lt; Previous &#160;
				</a>
			</p>
		</xsl:if>

		<xsl:if test="(not($QSpageindex) and count(rows/row) &gt; 10) or (($endPos) and number($endPos) &lt; count(rows/row))">
			<!-- There appear to be more posts...show a 'next' link -->
			<p>
				<a>
					<xsl:attribute name="href">?PageIndex=<xsl:value-of select="number($pos)+1"/></xsl:attribute>
					&#160;Next &gt;&gt;
				</a>
			</p>
		</xsl:if>

	</xsl:template>

	<xsl:template match="row">
		<xsl:variable name="publishedDate" select="date:parseDate(PublishedDate, 'yyyy-MM-dd HH:mm:ss')"/>

		<h2>
			<a title="{Title}" href="{ebe:createPostUrl(PublishedDate, BlogTitleForUrl)}">
				<xsl:value-of select="Title"/>
			</a>			
		</h2>
		
		<div id="content">
			
			<p class="postingdetail">
				<xsl:if test="CategoriesAsText != ''">
					<strong>Tags:</strong>&#160;
					 <xsl:for-each select="string:split(CategoriesAsText, ';')">
					 	<a href="{ebe:createTagUrl(.)}">
						 	<xsl:value-of select="."/>
					 	</a>
					 	<xsl:if test="position() != last()">&#160;&#183;&#160;</xsl:if>
					 </xsl:for-each>
				</xsl:if>
			</p>
	
			<xsl:if test="$IsBlogOwner = 'True'">
				<p>
					<a>
						<xsl:attribute name="href">
							/<xsl:value-of select="substring-after(FileDirRef,'#')"/>/EditPost.aspx?ID=<xsl:value-of select="ID"/>
						</xsl:attribute>
						[Edit Post]
					</a>
				</p>
			</xsl:if>
	
			<xsl:variable name="pureText">
				<xsl:call-template name="removeHtmlTags">
					<xsl:with-param name="html" select="Body" />
				</xsl:call-template>
			</xsl:variable>
	
			<div id="entry">
				<xsl:choose>
					<xsl:when test="$TrimPostBodyOnLists = 'True'" ><xsl:value-of select="substring($pureText, 0, 250)" disable-output-escaping="yes" />...</xsl:when>
					<xsl:otherwise><xsl:value-of select="Body" disable-output-escaping="yes"/></xsl:otherwise>
				</xsl:choose>						
			</div>
	
			<p class="postingdetail">
				<xsl:value-of select="date:format-date($publishedDate, 'MMMM d, yyyy')"/>
				&#183;
				Posted by <xsl:value-of select="substring-after(Author,'#')"/>
				&#183;
				<a href="{ebe:createPostUrl(PublishedDate, BlogTitleForUrl)}#postComments">
					<xsl:value-of select="NumComments"/> Comment<xsl:if test="NumComments != 1">s</xsl:if>
				</a>
			</p>
		</div>
	</xsl:template>

	<xsl:template name="removeHtmlTags">
		<xsl:param name="html"/>
		<xsl:choose>
			<xsl:when test="contains($html, '&lt;')">
				<xsl:value-of select="substring-before($html, '&lt;')"/>
				<xsl:call-template name="removeHtmlTags">
					<!-- Recurse through HTML -->
					<xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$html" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
